package main

import (
	"fmt"
	"sync"
)

var (
	myMp = make(map[int]int64, 10)
	//声明全局的互斥锁
	lock sync.Mutex
)

func test(num int) {

	var total int64 = 1
	for i := 1; i <= num; i++ {
		total *= int64(i)
	}
	lock.Lock()
	myMp[num] = total
	lock.Unlock()
}

//go build -race
func main() {
	//开启协程完成任务
	//fatal error: concurrent map writes
	for i := 1; i < 30; i++ {
		go test(i)
	}

	//遍历map时候可能存在资源竞争
	lock.Lock()
	for key, val := range myMp {
		fmt.Println(key, val)
	}
	lock.Unlock()
}
